package com.sencha.myblog;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DaoManager {
	private Map<String, User> users = Collections
			.synchronizedMap(new HashMap<String, User>());
	private Map<String, Preference> prefs = Collections
			.synchronizedMap(new HashMap<String, Preference>());
	private static DaoManager manager;

	private DaoManager() {
		// 1. initialize users
		users.put("user1", new User("user1", "password1"));
		users.put("user2", new User("user2", "password2"));

		// 2 initialize preference
		Preference pref1 = new Preference("老徐",
				"http://blog.sina.com.cn/rss/1190363061.xml");
		prefs.put("老徐", pref1);

		Preference pref2 = new Preference("豆瓣",
				"http://www.douban.com/feed/review/latest");
		prefs.put("豆瓣", pref2);

		Preference pref3 = new Preference("美女私房菜",
				"http://blog.sina.com.cn/rss/1494856974.xml");
		prefs.put("美女私房菜", pref3);

		Preference pref4 = new Preference("developerworks soa",
				"http://www.ibm.com/developerworks/views/webservices/rss/libraryview.jsp");
		prefs.put("developerworks soa", pref4);

		Preference pref5 = new Preference("developerworks cn",
				"http://www.ibm.com/developerworks/cn/views/global/rss/libraryview.jsp?end=10");
		prefs.put("developerworks cn", pref5);

		users.get("user1").getSubscriptions().add(pref1);
		users.get("user1").getSubscriptions().add(pref2);
		users.get("user1").getSubscriptions().add(pref3);
		users.get("user1").getSubscriptions().add(pref4);
		users.get("user1").getSubscriptions().add(pref5);

		users.get("user2").getSubscriptions().add(pref2);
		users.get("user2").getSubscriptions().add(pref3);
		users.get("user2").getSubscriptions().add(pref4);

	}

	public static synchronized DaoManager getInstance() {
		if (manager == null) {
			manager = new DaoManager();
		}
		return manager;
	}

	public List<Preference> getPreferences(String username) {
		return users.get(username).getSubscriptions();
	}

	public Preference getPreference(String title) {
		return prefs.get(title);
	}

	public User getUser(String username) {
		return users.get(username);
	}
}
